package com.fatri.lessionsimple.opengl.drawer

import android.content.Context
import android.opengl.GLES20
import com.fatri.lessionsimple.opengl.IDrawer
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer

/**
 * 基本的三角形
 */
class TriangleDrawer(ctx: Context) : IDrawer(ctx){
    private lateinit var vertexBuffer: FloatBuffer
    private val vertexShaderCode = "attribute vec4 vPosition;" +
            "void main() {" +
            "  gl_Position = vPosition;" +
            "}"

    private val fragmentShaderCode = "precision mediump float;" +
            "uniform vec4 vColor;" +
            "void main() {" +
            "  gl_FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);" +
            "}"

    private var mProgram = 0

    val COORDS_PER_VERTEX = 3
    var triangleCoords = floatArrayOf(
        0.5f, 0.5f, 0.0f,  // top
        -0.5f, -0.5f, 0.0f,  // bottom left
        0.5f, -0.5f, 0.0f // bottom right
    )

    private var mPositionHandle = 0
    private var mColorHandle = 0


    //顶点个数
    private val vertexCount = triangleCoords.size / COORDS_PER_VERTEX

    //一个顶点占有的总字节数
    private val vertexStride = COORDS_PER_VERTEX * 4 // 每个顶点包括3个float，一个float四字节

    //设置颜色，依次为红绿蓝和透明通道
    var color = floatArrayOf(1.0f, 1.0f, 1.0f, 1.0f)


    private fun initPos() {
        val bb = ByteBuffer.allocateDirect(
            triangleCoords.size * 4
        )
        bb.order(ByteOrder.nativeOrder())

        vertexBuffer = bb.asFloatBuffer()
        vertexBuffer.put(triangleCoords)
        vertexBuffer.position(0)
        val vertexShader = loadShader(
            GLES20.GL_VERTEX_SHADER,
            vertexShaderCode
        )
        val fragmentShader = loadShader(
            GLES20.GL_FRAGMENT_SHADER,
            fragmentShaderCode
        )


        //创建一个空的OpenGLES程序
        mProgram = GLES20.glCreateProgram()
        //将顶点着色器加入到程序
        GLES20.glAttachShader(mProgram, vertexShader)
        //将片元着色器加入到程序中
        GLES20.glAttachShader(mProgram, fragmentShader)
        //连接到着色器程序
        GLES20.glLinkProgram(mProgram)

    }

//    override fun onSurfaceChanged(width: Int, height: Int) {
//        val radio = width * 1.0f / height
//
//        //设置透视投影
//        Matrix.frustumM(mProjectMatrix, 0, -radio, radio, -1f, 1f, 3f, 7f)
//
//        //设置相机位置
//
//        Matrix.setLookAtM(
//            mViewMatrix, 0, 0f, 0f, 0f,
//            7.0f, 0f, 0f,
//            0f, 0f, 1.0f
//        )
//        //计算变换矩阵
//        Matrix.multiplyMM(mMVPMatrix, 0, mProjectMatrix, 0, mViewMatrix, 0)
//    }

    override fun onDrawFrame() {
        doDraw()
    }

    override fun onSurfaceCreated() {
        initPos()
    }


    private fun doDraw() {


        //将程序加入到OpenGLES2.0环境
        GLES20.glUseProgram(mProgram)

        //获取顶点着色器的vPosition成员句柄
        mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition")
        //启用三角形顶点的句柄
        GLES20.glEnableVertexAttribArray(mPositionHandle)
        //准备三角形的坐标数据
        GLES20.glVertexAttribPointer(
            mPositionHandle,  //属性角标，从0开始递增，可能有顶点、法线、纹理等
            COORDS_PER_VERTEX,  //一个顶点的数据个数，这里一个顶点包括三个浮点数
            GLES20.GL_FLOAT,    //数据类型
            false,  //是否需要显卡把数据归一化到（-1，1）区间
            vertexStride,      //字节步长，一个顶点占有的总字节数
            vertexBuffer
        )
        //获取片元着色器的vColor成员的句柄
//        mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor")
//        //设置绘制三角形的颜色
//        GLES20.glUniform4fv(mColorHandle, 1, color, 0)
        //绘制三角形
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount)
        //禁止顶点数组的句柄
        GLES20.glDisableVertexAttribArray(mPositionHandle)

    }


    override fun release() {
//        GLES20.glDisableVertexAttribArray(mPositionHandle)
//        GLES20.glDisableVertexAttribArray(mTexturePosHandler)
//        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0)
//        GLES20.glDeleteTextures(1, intArrayOf(mTextureID), 0)
//        GLES20.glDeleteProgram(mProgram)

    }

    override fun onSurfaceChanged(width: Int, height: Int) {
    }


}